﻿ 
/*
//function keeps track of side row vertices

        private void reAllocateVert(String side){

            int TRtmp = TRVerts[0];
            int BRtmp = BRVerts[0];
            int RRtmp = RRVerts[0];
            int LRtmp = LRVerts[0];

            switch (side)
            {
                case "LEFT":
                    //consider the old left side to be the new right side

                    TRtmp = TRVerts[0];
                    BRtmp = BRVerts[0];
                   
                    //move the left row of vertices to the other side of the terrain
                    for (int i = 0; i < VSegments; i++)
                    {

                        //left row becomes right row
                        RRVerts[i] = LRVerts[i];
                        //bottom and top rows move too
                        //ie [1,2,3,4] becomes [2,3,4,1]
                        //store first in tmp
                        
                        if (i < VSegments - 1)
                        {
                            TRVerts[i] = TRVerts[i + 1];
                            BRVerts[i] = BRVerts[i + 1];
                        }
                        else
                        {
                            TRVerts[i] = TRtmp;
                            BRVerts[i] = BRtmp;
                        }                  
                       
                        //move vertices to other side of terrain
                        vertices[LRVerts[i]].Position.X += terrainWidth;
                        vertices[LRVerts[i]].TextureCoordinate.X += HSegments;
                        //create new left row indexing
                        LRVerts[i] += 1;
                        if (LRVerts[i] == ((i+1) * HSegments ))
                        {
                            LRVerts[i] -= HSegments;
                        }                    

                        //move the indexbuffer key holdings 6 positions to the left (move the first 6 to the end of the index array)
                        //recalculate the finals (bind them to former right row
                      
                       
                    }
                    moveIndexBuffer(new Vector2(1, 0));
                    break;

                case "RIGHT":
                    //consider the old right side to be the new left side
                
                    
                    //move right row to left side
                    for (int i = 0; i < VSegments; i++)
                    {
                       

                        LRVerts[i] = RRVerts[i];
                        //move right vertices to other side of terrain
                        vertices[RRVerts[i]].Position.X -= terrainWidth;
                        vertices[RRVerts[i]].TextureCoordinate.X -= HSegments;
                        //create new left row indexing
                        RRVerts[i] -= 1;
                        if (RRVerts[i] == (i  * HSegments)-1)
                        {
                            RRVerts[i] += HSegments;
                        }
                   
                    }
                    moveIndexBuffer(new Vector2(-1, 0));
                    break;

                case "BOTTOM":
                    //consider the old right side to be the new left side


                    RRtmp = RRVerts[0];
                    LRtmp = LRVerts[0];

                    //move bottom row to the top position
                    for (int i = 0; i < HSegments; i++)
                    {
                      
                        //copy these verts to be the new top row
                        TRVerts[i] = BRVerts[i];

                        if (i < HSegments -1)
                        {
                            RRVerts[i] = RRVerts[i + 1];
                            LRVerts[i] = LRVerts[i + 1];
                        }
                        else
                        {
                            RRVerts[i] = RRtmp;
                            LRVerts[i] = LRtmp;
                        }
                   
                        //move right vertices to other side of terrain
                       
                        vertices[BRVerts[i]].Position.Z += terrainHeight;
                        vertices[BRVerts[i]].TextureCoordinate.Y += VSegments;
                        //create new bottom row indexing
                        BRVerts[i] += HSegments;
                        if (BRVerts[i] >= vertices.Length)
                        {
                            BRVerts[i] -= vertices.Length;                            
                        }

                    }
                    moveIndexBuffer(new Vector2(0, 1));
                    break;

                case "UP": 

                   
                    for (int i = 0; i < HSegments; i++)
                    {
                        //copy these verts to be the new top row
                        BRVerts[i] = TRVerts[i];
                        //move right vertices to other side of terrain
                     
                        vertices[TRVerts[i]].Position.Z -= terrainHeight;
                        vertices[TRVerts[i]].TextureCoordinate.Y -= VSegments;
                        //create new bottom row indexing
                        TRVerts[i] -= HSegments;
                        if (TRVerts[i] < 0)
                        {
                            TRVerts[i] += vertices.Length;
                        }

                    }
                    moveIndexBuffer(new Vector2(0, -1));
                    break;

            }
            /*
            Trace.WriteLine("LEFT ROW  : ");

            //reset the indexbuffer for proper drawing
           // for (int z = 0; z < VSegments; z++ )
         //   {
          //      Trace.WriteLine(LRVerts[z]);
          //  }

            Trace.WriteLine("RIGHT ROW  : ");

            //reset the indexbuffer for proper drawing
            for (int z = 0; z < VSegments; z++ )
            {
                Trace.WriteLine(RRVerts[z]);
            }

            Trace.WriteLine("TOP ROW  : ");
            //reset the indexbuffer for proper drawing
            for (int z = 0; z < HSegments; z++)
            {
                Trace.WriteLine(TRVerts[z]);
            }

            Trace.WriteLine("BOTTOM ROW  : ");
            //reset the indexbuffer for proper drawing
            for (int z = 0; z < HSegments; z++)
            {
                Trace.WriteLine(BRVerts[z]);
            }
           


            //reset the buffer with the new vertex positions
            //vertexBuffer = new VertexBuffer(Engine.GraphicsDevice, vertices.Length * VertexNPMTexture.SizeInBytes, BufferUsage.WriteOnly);
            //vertexBuffer.SetData(vertices);
        }


        private void moveIndexBuffer(Vector2 dir)
        {
             int count = 0;
            if (dir.X == 1)
            {
                //get the firs six indices, skip (HSegments - 1) * 6 , then replace these with 
                //all index key values go 1 up, but if it raises higher than the current row, go minus width
               
            
                    for (int y = 0; y < VSegments -1; y++)
                   {
                           for (int x = 0; x < (HSegments-1) * 6; x++)
                            {                           
                             
                              indices[count] += 1;
                                //ie : if 3 turns 4, turn 4 into 0. if 7 turn into an 8, turn 8 into a 4
                             if((indices[count] == HSegments * (y + 1)) || (indices[count] == (HSegments * (y+1))+ HSegments)){

                                   indices[count] -= HSegments;
                                    //set the new uv coordinates
                                   
                                  
                              }
                                count++;
                           }
                 }                

            }

            if (dir.X == -1)
            {
                //get the firs six indices, skip (HSegments - 1) * 6 , then replace these with 
                //all index key values go 1 up, but if it raises higher than the current row, go minus width
               

                for (int y = 0; y < VSegments - 1; y++)
                {
                    for (int x = 0; x < (HSegments - 1) * 6; x++)
                    {

                        indices[count] -= 1;
                        //ie : if 0 turns -1, turn -1 into 3. if 4 turn into an 3, turn 3 into a 7
                        if ((indices[count] == (y * HSegments) -1 ) || (indices[count] == ((HSegments * (y + 1))-1))){

                        

                            indices[count] += HSegments;
                           
                        }
                        count++;
                    }
                }

            }


            if (dir.Y == 1)
            {
                //get the firs six indices, skip (HSegments - 1) * 6 , then replace these with 
                //all index key values go 1 up, but if it raises higher than the current row, go minus width


                for (int y = 0; y < (HSegments - 1); y++)
                {
                    for (int x = 0; x < (VSegments - 1) * 6; x++)
                    {

                        indices[count] += HSegments;
                        //ie : if 16 turns 0, turn 17 into 1. if 18 turn into an 2, turn 19 into a 3
                        if (indices[count] >= vertices.Length)
                        {
                            indices[count] -= vertices.Length;


                        }
                        count++;
                    }
                }
            }

            if (dir.Y == -1)
            {
                //get the firs six indices, skip (HSegments - 1) * 6 , then replace these with 
                //all index key values go 1 up, but if it raises higher than the current row, go minus width
            

                for (int y = 0; y < (HSegments - 1); y++)
                {
                    for (int x = 0; x < (VSegments - 1) * 6; x++)
                    {

                        indices[count] -= HSegments;
                        //ie : if 3 turns 4, turn 4 into 0. if 7 turn into an 8, turn 8 into a 4
                        if (indices[count] <  0 )
                        {
                            indices[count] += vertices.Length;                           

                        }
                        count++;
                    }
                }

            }


            //indexBuffer = new IndexBuffer(Engine.GraphicsDevice, typeof(int), indices.Length, BufferUsage.WriteOnly);
           // indexBuffer.SetData(indices);


        }
}




*/